/*
//	CP2013 Traffic Simulator Assignment :: (MVC) View :: SDL Visualisation Window (Header File)
//	Copyright (c) 2010, Adam Rehn
//
//	This class is the application's visualisation window, implemented in SDL.
//	An instance is created within the main MVC View class.
*/

#ifndef _VISUALISATION_H
#define _VISUALISATION_H

//We are visualising the model
#include "../model/model.h"

//Include the main SDL header file
#include <SDL/SDL.h>

//The default cycle length (in seconds)
#define DEFAULT_CYCLE_LENGTH  0.5

//This creates a double-buffered, hardware-accelerated surface in main memory
#define WINDOW_FLAGS          SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_HWACCEL

//Macros for various RGB colour values
#define COLOUR_BLACK          0,   0,   0
#define COLOUR_WHITE          255, 255, 255
#define COLOUR_RED            255, 0,   0
#define COLOUR_BLUE           0,   0,   255
#define COLOUR_GREEN          0,   255, 0
#define COLOUR_YELLOW         255, 255, 0
#define COLOUR_ORANGE         255, 255, 127
#define COLOUR_BROWN          205, 100, 35
#define COLOUR_PURPLE         255, 0,   255
#define COLOUR_GREY           90,  90,  90
#define COLOUR_MEDIUMGREY     120, 120, 120
#define COLOUR_LIGHTGREY      210, 210, 210

#define TRANSPARENT_COLOUR    COLOUR_PURPLE

//The size (in square pixels) of one model unit as rendered in the visualisation
#define UNIT_TRANSLATION      1

class visualisationWindow
{
	public:
		visualisationWindow(unsigned int w, unsigned int h);
		~visualisationWindow();
		
		//Use this to reset the visualisation window size
		void setWindowSize(unsigned int w, unsigned int h);
		
		//Use this to set the cycle length in seconds
		void setCycleLength(double cycleLength);
		
		//The view can use these to update the display
		void compositeMap(mvcModel* model);
		void updateCarsAndLights(mvcModel* model);
		void renderToScreen(clock_t start = 0, unsigned int i = 0, unsigned int t = 0);
		
		//Use this to set the window title
		void setWindowTitle(char* title);
		
	private:
		//The visualisation window
		SDL_Surface* screen;
		
		//The layers of the visualisation
		SDL_Surface* streetMap;
		SDL_Surface* tileDirections;
		SDL_Surface* cars;
		SDL_Surface* trafficLights;
		
		//The vehicle sprites for each direction
		SDL_Surface* vehicleSprite_N;
		SDL_Surface* vehicleSprite_S;
		SDL_Surface* vehicleSprite_W;
		SDL_Surface* vehicleSprite_E;
		
		//The traffic light sprites for each direction
		SDL_Surface* trafficlightSprite_NS;
		SDL_Surface* trafficlightSprite_WE;
		
		//The length of a cycle in seconds
		double       cycleLength;
		
		//This holds colours for vehicles to be rendered with
		vector<Uint32> vechicleColours;
		
		//Helper functions
		void         freeSurfaces(bool freeSprites = false);
		SDL_Surface* createTransparentSurface();
		void         loadSpriteResources();
		SDL_Surface* createTransparentSurfaceFromResource(const char *ptr, size_t size);
		unsigned int toPx(unsigned int i);
		void         renderVehicleSprite(vehicle& currVehicle);
		void         renderTrafficLights(mvcModel* model, direction_t set);
};

#endif
